import { Component, OnInit } from '@angular/core';
import { UploadConfig, ToastService } from 'ng-tui';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { GlobalService } from '../../../../cores/services';
import { InventoryService } from '../../services/inventory.service';
import { Inventory } from '../../interfaces/inventory.interfaces';
import { ApiData } from '../../../../cores/classes';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    selector: 'app-inventory-detail',
    templateUrl: './inventory-detail.component.html',
    styleUrls: ['./inventory-detail.component.scss']
})
export class InventoryDetailComponent implements OnInit {

    inventory: Inventory = { id: 0, isActive: 1 };
    date = '';
    options: UploadConfig;

    constructor(
        public global: GlobalService,
        private active: ActivatedRoute,
        private toast: ToastService,
        private inventoryService: InventoryService,
    ) {
        this.options = {
            queryString: '?x-oss-process=image/resize,h_130,w_130',
            uploader: (file: File) => this.inventoryService.uploadCompayLogo(file)
        };
        this.active.paramMap
            .pipe(skipWhile(params => !params.has('id')), switchMap<ParamMap, ApiData>(params => {
                this.inventory.id = parseInt(params.get('id'), 10);
                return this.inventoryService.getCompany(this.inventory.id);
            }))
            .subscribe(res => this.inventory = res.datas);
    }

    ngOnInit() {
    }

    /**
     * 确认添加
     */
    // confirmInsert(btn: any) {
    //     this.inventoryService.insertCompany(this.inventory).subscribe({
    //         next: res => {
    //             this.toast.success('添加成功', `成功添加商户${this.inventory.companyName}`);
    //         },
    //         complete: () => {
    //             btn.dismiss();
    //         }
    //     });
    // }

    /**
     * 确认修改
     */
    confirmUpdate(btn: any) {
        this.inventoryService.updateCompany(this.inventory).subscribe({
            next: res => {
                this.toast.success('修改成功', `成功修改商户${this.inventory.companyName}的信息`);
            },
            complete: () => {
                btn.dismiss();
            }
        });
    }
}
